import {ElMessage, ElMessageBox} from "element-plus";

// 删除确认
function deleteConfirmation(id, remove) {
    ElMessageBox.confirm(
        '是否确认删除?',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            // 用户点击确定后的操作
            remove(id);
        })
        .catch(() => {
            // 用户点击取消后的操作
            ElMessage.info('已取消删除');
        });
}

// 验证数据
function validateForm(formRef, isSubmitDisabled) {
    if (formRef.value) {
        formRef.value.validate((valid) => {
            isSubmitDisabled.value = !valid; // 根据验证结果设置按钮禁用状态
        });
    }
}

function initializeCreateFormData(createFormItems, form) {
    createFormItems.forEach(item => {
        form[item.key] = item.default;
    });
}

function getEnumLabel(options, value) {
    const option = options.find(opt => opt.value === value);
    return option ? option.label : value;
}

function initializeRules(createFormItems, rules) {
    createFormItems.forEach(item => {
        if (item.required) {
            if (item.message === null) {
                item.message = `${item.label}不能为空`
            }
            rules[item.key] = [
                {required: true, message: item.message, trigger: 'blur'}
            ];
            if (item.type === 'number') {
                rules[item.key].push({type: 'number', message: '请输入数字类型', trigger: 'blur'})
            }
        }
    });
}


export {deleteConfirmation, validateForm, initializeCreateFormData, initializeRules, getEnumLabel};
